//
//  LVDownLoadManager.h
//  Live
//
//  Created by 唐开福 on 16/8/8.
//  Copyright © 2016年 Heller. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "YYCache.h"

typedef enum : NSInteger {
    LVDownloadStateNone = 0,// 闲置状态
    LVDownloadStateWillResume, // 即将下载（等待下载）
    LVDownloadStateResumed, // 下载中
    LVDownloadStateSuspened, // 暂停中
    LVDownloadStatedownLoadCompleted, // 下载完成
    LVDownloadStateCompleted, // 下载并解压完成
} LVDownloadState;

/**
 *  跟踪下载进度的Block回调
 *
 *  @param totalBytesWritten         【目前总共】写入的数据量
 *  @param totalBytesExpectedToWrite 【最终需要】写入的数据量
 */
typedef void (^LVDownloadProgressChangeBlock)(unsigned long long totalBytesWritten, unsigned long long totalBytesExpectedToWrite);

/**
 *  状态改变的Block回调
 *
 *  @param file 文件的下载路径
 *  @param error    失败的描述信息
 */
typedef void (^LVDownloadStateChangeBlock)(LVDownloadState state, NSString *file, NSError *error);

@interface LVDownLoadInfo : NSObject <NSCoding>
@property (nonatomic, copy  ) NSString                      *downloadName;/**< 下载名称 */
@property (nonatomic, copy  ) NSString                      *filename;/**< 文件名, 同时也是yycache的key */
@property (nonatomic, copy  ) NSString                      *downLoadingPath;/**< 文件下载路径， 临时路径 */
@property (nonatomic, copy  ) NSString                      *url;/**< 文件url */
@property (nonatomic, assign) BOOL                          manualCancel;/**< 是否手动取消 */
@property (nonatomic, assign) LVDownloadState               state;/** 下载状态 */
@property (nonatomic, assign) unsigned long long            totalBytesWritten;/**< 已下载的数量  */
@property (nonatomic, assign) unsigned long long            totalBytesExpectedToWrite;/**< 文件的总大小 */
@property (nonatomic, strong) NSError                       *error;/**< 下载的错误信息 */
@property (nonatomic, strong) LVDownloadProgressChangeBlock progressChangeBlock;
@property (nonatomic, strong) LVDownloadStateChangeBlock    stateChangeBlock;
@property (nonatomic, strong) NSURLSessionDataTask          *downloadtask;
@property (nonatomic, strong) NSData                        *resumeData;
@property (nonatomic, strong) NSOutputStream                *stream;

@end



@interface LVDownLoadManager : NSObject
@property (nonatomic, strong) NSOperationQueue             *queue;
@property (nonatomic, copy  ) NSString                     * cachePath;
@property (nonatomic, assign) NSInteger                    maxOperationCount;// 最大同时执行的个数 默认是2
@property (nonatomic, strong) NSMutableArray<LVDownLoadInfo   *> *downloadingArray;
@property (nonatomic, strong) NSMutableArray<LVDownLoadInfo   *> *waitArray;
@property (nonatomic, strong) YYCache          * cacheManager;// 用来管理下载的信息

/**
 *  创建下载器
 */
+ (instancetype)managerWithName:(NSString *)name;

/**
 *  是否是手动取消下载
 */
- (void)cancel:(NSString *)url isManualCancel:(BOOL)manual;

/**
 *  是否是手动取消所有的下载
 */
- (void)cancelAllIsManualCancel:(BOOL)manual;

/**
 *  是否是手动暂停下载, 会自动下载下一个
 */
- (void)suspend:(NSString *)url isManualCancel:(BOOL)manual;
/**
 *  是否手动暂停所有的下载, 下次可以调用resumeAll恢复之前的下载
 */
- (void)suspendAllIsManualCancel:(BOOL)manual;

/**
 *  恢复下载
 */
- (void)resume:(NSString *)url;

/**
 *  恢复所有的下载
 */
- (void)resumeAll;

/**
 *  根据url获取对应的下载信息
 */
- (LVDownLoadInfo *)downloadInfoForURL:(NSString *)url;
/**
 *  根据url获取文件的下载状态
 */
- (LVDownloadState)stateForDownloadUrl:(NSString *)url;

/**
 *  创建下载任务
 */
- (LVDownLoadInfo *)creatDownloadTask:(NSString *)url  progress:(LVDownloadProgressChangeBlock)progress state:(LVDownloadStateChangeBlock)state;


/**
 *  创建下载任务
 *
 *  @param url      URL
 *  @param name     下载任务的名字， 用于下载失败时候提示
 *
 */
- (LVDownLoadInfo *)creatDownloadTask:(NSString *)url name:(NSString *)name progress:(LVDownloadProgressChangeBlock)progress state:(LVDownloadStateChangeBlock)state;

/**
 *  删除下载信息
 *  @param isDelete 是否把下载的文件删除
 */
//- (void)deleteDownloadInfo:(NSString *)url shoudDeletedownloadData:(BOOL)isDelete;
@end
